#include "cv.h"
#include "highgui.h"
#include "frameop.h"
#include <stdio.h>
#include "constants.h"

int main ( int argc, char* argv[] )
{
	cvNamedWindow ( "display_orig", CV_WINDOW_AUTOSIZE );
	cvNamedWindow ( "display_bg", CV_WINDOW_AUTOSIZE );
	cvNamedWindow ( "display_sub", CV_WINDOW_AUTOSIZE );
	cvNamedWindow ( "display_thresh", CV_WINDOW_AUTOSIZE );
	
	CvCapture* capture = cvCreateFileCapture ( "/home/timkelch/test1" );
	
	
	
	if ( !capture )
	{
		return -1;
	}

	

	CvSize size = cvSize ( ( int ) cvGetCaptureProperty ( capture, CV_CAP_PROP_FRAME_WIDTH ), ( int ) cvGetCaptureProperty ( capture, CV_CAP_PROP_FRAME_HEIGHT ));
	
	frameOp f;
	f.init(size);

	IplImage* cFrame = cvCreateImage (size,IPL_DEPTH_8U,1);
	cvSet(cFrame,cvScalar(127));
	
	IplImage* cFrame_rgb;
	
	
	while ( ( cFrame_rgb=cvQueryFrame ( capture ) ) != NULL )
	{
		
		cvConvertImage (cFrame_rgb, cFrame,0);
		cvShowImage ( "display_orig", cFrame);
		
		f.updateBackground(cFrame);
		cvShowImage( "display_bg", f.bFrame);
		
		//f.subtractFrame(cFrame);
		cvAbsDiff(cFrame, f.bFrame, cFrame);
		cvShowImage("display_sub", cFrame);
		
		IplConvKernel* element = NULL;
		cvThreshold(cFrame, cFrame, 25,255,CV_THRESH_BINARY);
		cvErode(cFrame, cFrame,element);
		cvDilate(cFrame, cFrame, element);
		
		cvShowImage("display_thresh", cFrame);
		
		
		char c = cvWaitKey ( 30 );
		
		if ( c == 27 ) break;
		
	}

	cvReleaseImage ( &cFrame );
	
	cvReleaseImage ( &cFrame_rgb );

	cvReleaseCapture ( &capture );
	
	return 1;
}
